<!DOCTYPE html>

<html>
<head>
  <title>database.coffee</title>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <link rel="stylesheet" media="all" href="public/stylesheets/normalize.css" />
  <link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
  <div class="container">
    <div class="page">

      <div class="header">
        
          <h1>database.coffee</h1>
        

        
          <div class="toc">
            <h3>Table of Contents</h3>
            <ol>
              
                
                <li>
                  <a class="source" href="database.html">
                    database.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="manager.html">
                    manager.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="routes.html">
                    routes.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="security.html">
                    security.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="sockets.html">
                    sockets.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="sync.html">
                    sync.coffee
                  </a>
                </li>
              
            </ol>
          </div>
        
      </div>

      
        
        <h2 id="server-database">SERVER DATABASE</h2>

        
      
        
        <p>Handles the database interactions on the app.</p>

        
          <div class='highlight'><pre><span class="class"><span class="keyword">class</span> <span class="title">Database</span></span></pre></div>
        
      
        
        <p>Require Expresser.</p>

        
          <div class='highlight'><pre>    expresser = <span class="built_in">require</span> <span class="string">"expresser"</span>
    settings = expresser.settings</pre></div>
        
      
        
        <h2 id="entity-definitions-and-objects">ENTITY DEFINITIONS AND OBJECTS</h2>

        
      
        
        <p>Get <a href="entityDefinition.html">Entity Definitions</a>.</p>

        
          <div class='highlight'><pre>    <span class="attribute">getEntityDefinition</span>: <span class="function"><span class="params">(filter, callback)</span> =&gt;</span>
        expresser.database.get <span class="string">"entity"</span>, filter, callback</pre></div>
        
      
        
        <p>Insert or update an <a href="entityDefinition.html">Entity Definition</a>.</p>

        
          <div class='highlight'><pre>    <span class="attribute">setEntityDefinition</span>: <span class="function"><span class="params">(obj, options, callback)</span> =&gt;</span>
        expresser.database.set <span class="string">"entity"</span>, obj, options, callback</pre></div>
        
      
        
        <p>Delete the specified <a href="entityDefinition.html">Entity Definition</a>.</p>

        
          <div class='highlight'><pre>    <span class="attribute">deleteEntityDefinition</span>: <span class="function"><span class="params">(id, callback)</span> =&gt;</span>
        expresser.database.del <span class="string">"entity"</span>, id, callback</pre></div>
        
      
        
        <h2 id="maps">MAPS</h2>

        
      
        
        <p>Get <a href="map.html">Maps</a>.</p>

        
          <div class='highlight'><pre>    <span class="attribute">getMap</span>: <span class="function"><span class="params">(filter, callback)</span> =&gt;</span>
        expresser.database.get <span class="string">"map"</span>, filter, callback</pre></div>
        
      
        
        <p>Insert or update a <a href="map.html">Map</a></p>

        
          <div class='highlight'><pre>    <span class="attribute">setMap</span>: <span class="function"><span class="params">(obj, options, callback)</span> =&gt;</span>
        expresser.database.set <span class="string">"map"</span>, obj, options, callback</pre></div>
        
      
        
        <p>Delete the specified <a href="map.html">Map</a>.</p>

        
          <div class='highlight'><pre>    <span class="attribute">deleteMap</span>: <span class="function"><span class="params">(id, callback)</span> =&gt;</span>
        expresser.database.del <span class="string">"map"</span>, id, callback</pre></div>
        
      
        
        <h2 id="audit-data">AUDIT DATA</h2>

        
      
        
        <p>Get <a href="auditData.html">Audit Data records</a>.</p>

        
          <div class='highlight'><pre>    <span class="attribute">getAuditData</span>: <span class="function"><span class="params">(filter, callback)</span> =&gt;</span>
        expresser.database.get <span class="string">"auditdata"</span>, filter, callback</pre></div>
        
      
        
        <p>Insert or update an <a href="auditData.html">AuditData</a>.</p>

        
          <div class='highlight'><pre>    <span class="attribute">setAuditData</span>: <span class="function"><span class="params">(obj, options, callback)</span> =&gt;</span>
        expresser.database.set <span class="string">"auditdata"</span>, obj, options, callback</pre></div>
        
      
        
        <p>Delete the specified <a href="auditData.html">AuditData</a>.</p>

        
          <div class='highlight'><pre>    <span class="attribute">deleteAuditData</span>: <span class="function"><span class="params">(id, callback)</span> =&gt;</span>
        expresser.database.del <span class="string">"auditdata"</span>, id, callback</pre></div>
        
      
        
        <h2 id="audit-events">AUDIT EVENTS</h2>

        
      
        
        <p>Get <a href="auditEvent.html">Audit Events</a>.</p>

        
          <div class='highlight'><pre>    <span class="attribute">getAuditEvent</span>: <span class="function"><span class="params">(filter, callback)</span> =&gt;</span>
        expresser.database.get <span class="string">"auditevent"</span>, filter, callback</pre></div>
        
      
        
        <p>Insert or update an <a href="auditEvent.html">AuditEvent</a>.</p>

        
          <div class='highlight'><pre>    <span class="attribute">setAuditEvent</span>: <span class="function"><span class="params">(obj, options, callback)</span> =&gt;</span>
        expresser.database.set <span class="string">"auditevent"</span>, obj, options, callback</pre></div>
        
      
        
        <p>Delete the specified <a href="auditEvent.html">AuditEvent</a>.</p>

        
          <div class='highlight'><pre>    <span class="attribute">deleteAuditEvent</span>: <span class="function"><span class="params">(id, callback)</span> =&gt;</span>
        expresser.database.del <span class="string">"auditevent"</span>, id, callback</pre></div>
        
      
        
        <h2 id="variables">VARIABLES</h2>

        
      
        
        <p>Get <a href="variable.html">Variables</a>.</p>

        
          <div class='highlight'><pre>    <span class="attribute">getVariable</span>: <span class="function"><span class="params">(filter, callback)</span> =&gt;</span>
        expresser.database.get <span class="string">"variable"</span>, filter, callback</pre></div>
        
      
        
        <p>Insert or update a <a href="variable.html">Variable</a>.</p>

        
          <div class='highlight'><pre>    <span class="attribute">setVariable</span>: <span class="function"><span class="params">(obj, options, callback)</span> =&gt;</span>
        expresser.database.set <span class="string">"variable"</span>, obj, options, callback</pre></div>
        
      
        
        <p>Delete the specified <a href="variable.html">Variable</a>.</p>

        
          <div class='highlight'><pre>    <span class="attribute">deleteVariable</span>: <span class="function"><span class="params">(id, callback)</span> =&gt;</span>
        expresser.database.del <span class="string">"variable"</span>, id, callback</pre></div>
        
      
        
        <h2 id="users">USERS</h2>

        
      
        
        <p>Get <a href="user.html">User</a>.</p>

        
          <div class='highlight'><pre>    <span class="attribute">getUser</span>: <span class="function"><span class="params">(filter, callback)</span> =&gt;</span>
        expresser.database.get <span class="string">"user"</span>, filter, callback</pre></div>
        
      
        
        <p>Insert or update a <a href="user.html">User</a>.</p>

        
          <div class='highlight'><pre>    <span class="attribute">setUser</span>: <span class="function"><span class="params">(obj, options, callback)</span> =&gt;</span>
        expresser.database.set <span class="string">"user"</span>, obj, options, callback</pre></div>
        
      
        
        <p>Delete the specified <a href="user.html">User</a>.</p>

        
          <div class='highlight'><pre>    <span class="attribute">deleteUser</span>: <span class="function"><span class="params">(id, callback)</span> =&gt;</span>
        expresser.database.del <span class="string">"user"</span>, id, callback</pre></div>
        
      
        
        <h2 id="history-logs">HISTORY LOGS</h2>

        
      
        
        <p>Every insert and update on the database will be logged to a &quot;logs&quot; collection
and kept saved for a few hours. This is NOT related to general logging, for
this please check the <a href="logger.html">Logger</a> class.</p>

        
          <div class='highlight'><pre>    <span class="attribute">log</span>: <span class="function"><span class="params">(obj, options)</span> =&gt;</span>
        <span class="keyword">return</span> <span class="keyword">if</span> <span class="keyword">not</span> obj?</pre></div>
        
      
        
        <p>Append timestamp and options, if any.</p>

        
          <div class='highlight'><pre>        obj.timestamp = <span class="keyword">new</span> Date()
        obj.setOptions = options <span class="keyword">if</span> options?

        expresser.database.db.collection(<span class="string">"log"</span>).insert obj</pre></div>
        
      
        
        <p>Delete old log data. The maximum log age is defined at the <a href="settings.html">Server Settings</a>.
Default interval is every 2 hours.</p>

        
          <div class='highlight'><pre>    <span class="attribute">cleanLogs</span>:<span class="function"> =&gt;</span>
        minDate = <span class="keyword">new</span> Date()
        minDate.setHours(minDate.getHours() - settings.database.logExpires)

        expresser.database.db.collection(<span class="string">"log"</span>).remove {<span class="string">"timestamp"</span>: {<span class="string">"$lt"</span>:minDate}}
        expresser.logger.info <span class="string">"Deleted DB logs older than <span class="subst">#{minDate}</span>."</span></pre></div>
        
      
        
        <h2 id="helper-methods">HELPER METHODS</h2>

        
      
        
        <p>Clean a specific object and make it valid for MongoDB insertion.</p>

        
          <div class='highlight'><pre>    <span class="attribute">cleanObjectForInsertion</span>: <span class="function"><span class="params">(obj)</span> =&gt;</span>
        <span class="keyword">for</span> k <span class="keyword">of</span> obj
            <span class="keyword">if</span> obj.hasOwnProperty k
                <span class="keyword">if</span> k.indexOf(<span class="string">"."</span>) &gt;= <span class="number">0</span> <span class="keyword">or</span> k.indexOf(<span class="string">"$"</span>) &gt;= <span class="number">0</span>
                    <span class="keyword">delete</span> obj[k]
                <span class="keyword">else</span> <span class="keyword">if</span> <span class="keyword">typeof</span> obj[k] <span class="keyword">is</span> <span class="string">"object"</span>
                    obj[k] = <span class="property">@cleanObjectForInsertion</span> obj[k]
        <span class="keyword">return</span> obj</pre></div>
        
      
        
        <h2 id="singleton-implementation-">Singleton implementation.</h2>

        
      
        
        
        
          <div class='highlight'><pre>Database.<span class="function"><span class="title">getInstance</span> = -&gt;</span>
    <span class="property">@instance</span> = <span class="keyword">new</span> Database() <span class="keyword">if</span> <span class="keyword">not</span> <span class="property">@instance</span>?
    <span class="keyword">return</span> <span class="property">@instance</span>

module.<span class="built_in">exports</span> = <span class="built_in">exports</span> = Database.getInstance()</pre></div>
        
      
      <div class="fleur">h</div>
    </div>
  </div>
</body>
</html>
